import {
  PRODUCT_INFO,
  PRODUCT_CLEAR,
  PRODUCT_LIKE_ADD,
  PRODUCT_LIKE_CANCEL,
  PRODUCT_LIKE_LIST,
  PRODUCT_MEDIA_LIST,
  PRODUCT_CATE_LIST,
  PRODUCT_BRAND_LIST
} from '../constants/product'
import {
  CATE_SUB_LIST,
  CATE_SUB_LIST_CLEAR, CATE_CHANGE_MENU, CATE_HEIGHT_CLEAR
} from '../constants/cate'

const INITIAL_STATE = {
  productInfo: {},
  like: 0,
  likeList: [],
  mediaList: [],
  cates: [],
  brands: [],
  subList: [],
  total: 0,
  heightArr: [],
  masonryList: []
}
export default function product(state = INITIAL_STATE, action) {
  switch (action.type) {
    case PRODUCT_INFO: {
      const productInfo = action.payload
      return {...state, productInfo, like: productInfo.like}
    }
    case PRODUCT_CLEAR:
      const productInfo = {}
      const like = 0
      return {...state, productInfo, like}
    case PRODUCT_LIKE_ADD:
      return {...state, like: 1}
    case PRODUCT_LIKE_CANCEL:
      return {...state, like: 0}
    case PRODUCT_LIKE_LIST:
      let arr = [...action.payload]
      return {...state, likeList: arr}
    case PRODUCT_MEDIA_LIST:
      return {...state, mediaList: action.payload}
    case PRODUCT_CATE_LIST:
      return {...state, cates: action.qianqian.cates}
    case PRODUCT_BRAND_LIST:
      return {...state, cates: action.qianqian.brands}
    case CATE_SUB_LIST: {
      const {list, pagination, heightArr, masonryList} = action.qianqian
      console.log('action.qianqian', action.qianqian)
      return {
        ...state,
        subList: list,
        heightArr,
        masonryList: masonryList,
        total: pagination.total
      }
    }
    case CATE_SUB_LIST_CLEAR: {
      return {
        ...state,
        subList: [],
        total: 0,
        heightArr: []
      }
    }
    case CATE_HEIGHT_CLEAR: {
      return {
        ...state,
        heightArr: [],
        masonryList: []
      }
    }
    default:
      return state
  }
}
